<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp"%>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>


<div class="row">
	<div class="col-md-5">
		<div class="box box-primary">
			<div class="box-header with-border">
				<h3 class="box-title"></h3>
				<div class="box-tools pull-right" id="roleGridTB_${uuid}">
					<shiro:hasPermission name="ROLE_ADD">
						<button type="submit" class="btn btn-sm btn-success addBtn">
							<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
							添加
						</button>
					</shiro:hasPermission>

					<shiro:hasPermission name="ROLE_MOD">
						<button type="submit" class="btn btn-sm btn-primary editBtn">
							<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
							修改
						</button>
					</shiro:hasPermission>

					<shiro:hasPermission name="ROLE_DEL">
						<button type="submit" class="btn btn-sm btn-danger delBtn">
							<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
							删除
						</button>
					</shiro:hasPermission>
					
					<shiro:hasPermission name="ROLE_MOD">
						<div class="btn-group">
						  <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
						   	 交换 <span class="caret"></span>
						  </button>
						  <ul class="dropdown-menu">
						    <li><a href="javascript:void(0)" class="expBtn">导出</a></li>
						    <li><a href="javascript:void(0)" class="impBtn" id="impRoleBtn">导入</a></li>
						  </ul>
						</div>
					</shiro:hasPermission>
				</div>
			</div>
			<div class="box-body">
				<table id="roleGrid_${uuid}">

				</table>
			</div>
			<!-- /.box-body -->
		</div>
	</div>
	<div class="col-md-7">
		<div class="box box-primary">
			<div class="box-header with-border">
				<h3 class="box-title"></h3>
				<div class="box-tools pull-right" id="funcPointGridTB_${uuid}">
					<shiro:hasPermission name="ROLE_REL_FUNCPOINT">
						<button type="submit" class="btn btn-sm btn-success addBtn">
							<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
							关联功能点
						</button>
					</shiro:hasPermission>

					<shiro:hasPermission name="ROLE_REMOVE_FUNCPOINT">
						<button type="submit" class="btn btn-sm btn-danger delBtn">
							<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
							移除
						</button>
					</shiro:hasPermission>
				</div>
			</div>
			<div class="box-body">
				<table id="funcPointGrid_${uuid}">

				</table>
			</div>
			<!-- /.box-body -->
		</div>
	</div>
	<script type="text/javascript">

    $(function () {
        var uuid = '${uuid}';
        var roleGrid = $("#roleGrid_"+uuid);
        var roleGridTb=$("#roleGridTB_"+uuid);

        var funcPointGrid = $("#funcPointGrid_"+uuid);
        var funcPointGridTB = $("#funcPointGridTB_"+uuid);

        var roleIndexView = $("#roleIndex_"+uuid);
        
        roleGrid = roleGrid.bootstrapTable({
        	url:ctx + "/roleAction/queryRoles.do",
        	filterControl:true,
        	clickToSelect:false,
            columns: [
					{field: '_id',checkbox:true},
                    {title: '角色名称', field: 'roleName', width: 120,filterControl:"input"},
                    {title: '标识', field: 'roleKey',width: 300,filterControl:"input"},
                    {title: '默认', field: 'isDefault',width: 80, formatter:function (value,row, index) {
                            if (value == 1) {
                                return "是";
                            } else {
                                return "";
                            }
                        }}
                ],
                onClickRow:function(row){
                	var id=row.id;
            		funcPointGrid.roleId=id;
            		funcPointGrid.bootstrapTable("refresh");
                }
        });
        
        
        roleGridTb.find(".addBtn").click(function(){
        	var sub = top.webUtils.topicSubscribe("role.edit.success", function (args) {
                dialog.close();
                roleGrid.bootstrapTable("refresh");
            });
            var dialog = editRole({
                onClose: function () {
                    sub.remove();
                }
            });
        });
        
        roleGridTb.find(".editBtn").click(function(){
        	var rows=roleGrid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		var sub = top.webUtils.topicSubscribe("role.edit.success", function (args) {
                    dialog.close();
                    roleGrid.bootstrapTable("refresh");
                });
        		var row=rows[0];
        		var dialog= editRole({
                    id: row.id,
                    onClose: function () {
                        sub.remove();
                    }
                });
        	}else {
        		webUtils.alert("请选择需要编辑的数据");
            }
        });
        
        roleGridTb.find(".delBtn").click(function(){
        	var rows=roleGrid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var selecteds = [];
		                $.each(rows, function (i, n) {
		                    selecteds.push(n.id);
		                });
		                $.post(ctx + "/roleAction/delRole.do", {
		                    ids: selecteds.join(",")
		                }, function (data) {
		                    if (data.success) {
		                    	roleGrid.bootstrapTable("refresh");
		                    } else {
		                    	webUtils.alert(data.msg);
		                    }
		                }, 'json');
					}
        		});
        		
        	}else {
        		webUtils.alert("请选择需要编辑的数据");
            }
        });
        
        roleGridTb.find(".expBtn").click(function(){
        	var rows=roleGrid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		var ids = [];
                $.each(rows, function (i, n) {
                	ids.push(n.id);
                });
        		window.open(ctx + "/roleAction/export.do?ids="+ids.join(","));
        	} else {
        		webUtils.alert("请选择需要导出的数据");
            }
        });
        
        var uploader
        webUtils.fileUploader({
        	server:ctx+"/roleAction/import.do",
        	uploadPick:"#impRoleBtn",
        	duplicate:false,
        	accept: {
                title: 'json文件类型',
                //需要与platform.upload.allowFileTypes配置保持一致
                extensions: 'json'
            },
            afterInit:function(u,uder){
            	uploader=u;
            }
        });
        
        roleGridTb.find(".impBtn").mouseenter(function(){
        	uploader.refresh();
        });

        var editRole = function (params) {
            params = params || {};
            var t = [];
            if (params.id) {
                t.push("id=" + params.id);
            }
            var dialog=webUtils.dialog({
           	 title:'编辑角色',
           	 url:ctx + '/roleAction/editView.do?' + t.join("&"),
           	 onremove:function () {
                    if (params.onClose) {
                        params.onClose.call(null);
                    }
                }
            });
            return dialog;
        };


        //功能点定义部分
        funcPointGrid = funcPointGrid.bootstrapTable({
        	url:ctx + "/funcPointAction/queryFuncPointsInRole.do",
        	queryParams:function(d){
        		if(funcPointGrid && funcPointGrid.roleId){
					d.roleId=funcPointGrid.roleId;
	    		}
				return d;
        	},
            columns: [
					{field: '_id',checkbox:true},
                    {title: '功能点名称', field: 'name', width: 200},
                    {title: '功能点操作', field: 'optAuths', width: 300, formatter: function (value,row, index) {
                            var r = [];
                            $.each(value, function (i, n) {
                                r.push('<div style="float:left;width:80px"><input type="checkbox" name="actionAuth" value="' + n.id + '" ' + (n.roleId ? 'checked=\"checked\"' : '') + '/>' + n.name + '</div>');
                            });
                            r.push("<div style='clear:both;'></div>");
                            return r.join("");
                        }}
                ],
            onLoadSuccess: function () {
                $("input[name=actionAuth]").click(function () {
                    _this = $(this);
                    if (funcPointGrid.roleId) {
                        if (_this.is(":checked")) {
                            $.post(ctx + "/optAuthAction/setInRole.do", {roleId: funcPointGrid.roleId, ids: _this.val()});
                        } else {
                            $.post(ctx + "/optAuthAction/removeFromRole.do", {roleId: funcPointGrid.roleId, ids: _this.val()});
                        }
                    } else {
                        webUtils.alert("请选择具体的角色");
                        return false;
                    }
                });
            }
        });
        
        funcPointGridTB.find(".addBtn").click(function(){
        	if (funcPointGrid.roleId == null) {
                webUtils.alert("请选择具体的角色");
                return;
            }
        	
        	var sub = top.webUtils.topicSubscribe("funcPointForRole.selected", function (data) {
                $.post(ctx + "/funcPointAction/setInToRole.do", {
                    ids: data.join(","),
                    roleId: funcPointGrid.roleId
                }, function (result) {
                    dialog.close();
                    funcPointGrid.bootstrapTable("refresh");
                }, 'json');
            });
        	
        	var dialog=webUtils.dialog({
              	 title:'关联功能点',
              	 url:ctx + '/funcPointAction/listForRoleView.do?roleId=' + funcPointGrid.roleId,
              	 onremove:function () {
              		sub.remove();
                   }
               });
        	
        });
		funcPointGridTB.find(".delBtn").click(function(){
			webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
				if(result){
					if (funcPointGrid.roleId == null) {
		                webUtils.alert("请选择具体的角色");
		                return;
		            }
					var rows=funcPointGrid.bootstrapTable("getSelections");
		        	if(rows && rows.length>0){
						var selecteds = [];
		                $.each(rows, function (i, n) {
		                    selecteds.push(n.id);
		                });
		                $.post(ctx + "/funcPointAction/removeFromRole.do", {
		                    ids: selecteds.join(","),
		                    roleId: funcPointGrid.roleId
		                }, function (data) {
		                    funcPointGrid.bootstrapTable("refresh");
		                }, "json");
		        	}else{
		        		webUtils.alert("请选择需要移除的数据");
		        	}
				}
			});
			
        });
        
    });

</script>